package com.demo.servlet.demo1221.demo02.servlet;

import com.demo.servlet.demo1221.demo02.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/demo1221/demo02/user.do")
public class UserServlet extends BaseServlet {
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取登录的用户名
        String username = request.getParameter("username");
        //获取在线名单
        Map<String, User> onlineUsers = (Map<String, User>) this.getServletContext().getAttribute("onlineUsers");
        //如果在线名单已经存在
        if (onlineUsers != null) {
            //如果 在线名单中已经存在该用户名
            if (onlineUsers.containsKey(username)) {
                //请求转发到登录页面，显示错误信息
                request.setAttribute("msg", username + "已经登录！请勿重复登录！");
                request.getRequestDispatcher("./login.jsp").forward(request, response);
            }
        }
        //获取IP地址
        String ip = request.getRemoteHost();
        //获取登录时间
        String loginTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        //创建 User 对象
        User user = new User(username, ip, loginTime);
        //把 User 对象保存到 Session 当中
        request.getSession().setAttribute("user", user);
        //设置当前用户的Session
        user.setSession(request.getSession());
        //通过重定向 直接跳转到 users.jsp
        response.sendRedirect("./users.jsp");
    }

    protected void quit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将登录的用户信息移除
        request.getSession().removeAttribute("user");
        request.setAttribute("msg", "退出成功！");
        request.getRequestDispatcher("./login.jsp").forward(request, response);
    }

    protected void kick(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String ip = request.getRemoteHost();
//        if (!"192.168.4.36".equals(ip)) {
//            response.getWriter().write("您没有踢人的权限！");
//            return;
//        }
        String username = request.getParameter("username");
        //从当前Session获取当前用户信息
        User currentUser = (User) request.getSession().getAttribute("user");
        //如果当前用户名和踢人的用户名是同一个
        if (currentUser.getUsername().equals(username)) {
            response.getWriter().write("不允许自己踢自己！");
            return;
        }
        //从在线名单中获取被踢的用户
        Map<String, User> onlineUsers = (Map<String, User>) this.getServletContext().getAttribute("onlineUsers");
        User user = onlineUsers.get(username);
        //获取当前用户的Session
        user.getSession().removeAttribute("user");
        user.getSession().invalidate();
        //重定向到user.jsp 显示在线用户名单
        response.sendRedirect("./user.jsp");
    }

}
